<?php
/**
 * Created by PhpStorm.
 * User: dingjinlai
 * Date: 2018/10/19
 * Time: 下午4:09
 */

class Cne_api
{
    
    public $icID = 17218;
    
    public $secretKey = 'r3Qz0IahRi4qQqA';
    
    public $api_url = 'https://api.cnexps.com/cgi-bin/EmsData.dll?DoApi';
    
    //public  $api_url = 'https://api.cnexps.com/cgi-bin/EmsData.dll?DoApi&{"RequestName":"EmsKindList"}';
    
    public $cne_config = array();
    
    public function __construct()
    {
    
    }
    
    public function PreInputSet($order = array())
    {
        if ($order['shipping_no']) {
            $cno = $order['shipping_no'];
        } else {
            $goods_list = array();
    
            $i = 0;
            foreach ($order['order_goods'] as $row) {
        
                $i++;
        
                $goods_list[] = array(
                    'cxGoods' => $row['cat_name_zh'],
                    'cxGoodsA' => $row['cat_name'],
                    'cxGCodeA' => $row['goods_attr'],
                    'ixQuantity' => 1,
                    'fxPrice' => sprintf('%.2f', mt_rand(10, 26))
                );
        
                if ($i > 5) {
                    break;
                }
            }
    
    
            $timeStamp = $this->cne_curl($this->api_url, array('RequestName' => 'TimeStamp'));
            $this->cne_config = array(
                'RequestName' => 'PreInputSet',
                'icID' => $this->icID,
                'TimeStamp' => $timeStamp['ReturnValue'],
                'MD5' => md5($this->icID . $timeStamp['ReturnValue'] . $this->secretKey),
                'RecList' => array(
                    'iID' => 0,
                    'nItemType' => 1,
                    'cRNo' => $order['order_sn'],
                    'cDes' => $order['country_code'],
                    'cEmsKind' => 'SH国际E邮宝',
                    'fWeight' => 0.320,
                    'cReceiver' => $order['user_name'],
                    'cRPhone' => $order['phone'],
                    'cREMail' => $order['email'],
                    'cRPostcode' => $order['zip'],
                    'cRProvince' => $order['province'],
                    'cRCountry' => $order['country'],
                    'cRCity' => $order['city'],
                    'cRAddr' => $order['address'],
                    'GoodsList' => $goods_list
                )
            );
    
            /*$cne = $this->cne_curl($this->api_url, array(
                'RequestName' => 'EmsKindList',
                'icID' => $this->icID,
                'TimeStamp' => $timeStamp['ReturnValue'],
                'MD5' => md5($this->icID.$timeStamp['ReturnValue'].$this->secretKey)));*/
    
            $cne = $this->cne_curl($this->api_url, $this->cne_config);

            $cno = $cne['ErrList'][0]['cNum'];
        }
        $cne['url'] = $cno ? ('http://label.cnexps.com/CnePrint?icID=' . $this->icID . '&cNos=' . $cno . '&ptemp=label10x&signature=' . md5($this->icID.$cno.$this->secretKey)) : '';

        return $cne;
    }
    
    public function cne_curl($url, $post_data = array(), $method = 'POST')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        
        if ($post_data) {
            $data_string = json_encode($post_data);
            
            $headers = array(
                "Content-Type: application/json",
                "Accept: application/json",
                "Content-Length:" . strlen($data_string)
            );
            
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
        
        $content = curl_exec($ch);
        
        curl_close($ch);
        
        return json_decode($content, true);
    }
}